package com.atbeijing.D03;

import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * 富函数
 */
public class Example2 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(2);

        DataStreamSource<Integer> stream = env.fromElements(1, 2, 3, 4);

        stream.map(new RichMapFunction<Integer, Integer>() {
            //map开始前执行一次
            @Override
            public void open(Configuration parameters) throws Exception {
                super.open(parameters);
                System.out.println("open函数");
            }

            @Override
            public Integer map(Integer value) throws Exception {
                //System.out.println(getRuntimeContext().getIndexOfThisSubtask());
                return value+1;
            }

            //map结束后执行一次
            @Override
            public void close() throws Exception {
                super.close();
                System.out.println("close函数");
            }
        }).print();

        env.execute();
    }
}
